package com.example.huwang.utils;

import android.annotation.SuppressLint;
import android.app.Application;

import java.lang.reflect.Method;

/**
 * Created by 张柱 on 2022/4/11
 */
public class AppGlobals {
    private static Application sApplication;

    @SuppressLint("PrivateApi")
    public static Application getApplication() {
        if (sApplication == null) {
            //去反射得到
            try {
                Class<?> aClass = Class.forName("android.app.ActivityThread");
                //获取里面的currentApplication
                Method currentApplication = aClass.getDeclaredMethod("currentApplication");
                sApplication = (Application) currentApplication.invoke(null, null);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return sApplication;
    }
}
